<?php
// Copyright (c) 2009, 백괴사전 all rights reserved.
// Distributed under GNU General Public License v3.
// Read LICENSE for more information.

final class ChoseongTrifler {
	
	public static function Trifle($upclTextStr) {
		$upclLengthInt=strlen($upclTextStr)-1;
		
		$upclConvArr=array(array(1,2,4,7,5=>8,16,17,9=>19,20,12=>22,14=>23,24,25,26),1=>array(9=>3),4=>array(12=>5),8=>array(9,6=>10,11,9=>12,16=>13,14),17=>array(9=>18));
		
		for($upclLoopIndexInt=0;$upclLoopIndexInt<$upclLengthInt;) {
			switch(UTF8::GetCharLength($upclTextStr[$upclLoopIndexInt])) {
				case 3:
					if(isset($upclTextStr[$upclLoopIndexInt+3])===true) {
						switch(UTF8::GetCharLength($upclTextStr[$upclLoopIndexInt+3])) {
							case 3:
								$upclHan1Char=substr($upclTextStr,$upclLoopIndexInt,3);
								$upclHan2Char=substr($upclTextStr,$upclLoopIndexInt+3,3);
								if(Hangul::IsHangul($upclHan1Char)===true && Hangul::IsHangul($upclHan2Char)===true) {
									$upclHan1Arr=Hangul::Disorganize(UTF8::Char2Int($upclHan1Char));
									$upclHan2Arr=Hangul::Disorganize(UTF8::Char2Int($upclHan2Char));
									
									if(isset($upclConvArr[$upclHan1Arr[2]][$upclHan2Arr[0]])===true) {
										if(array_search($upclHan2Arr[0],array(3,16))!==false && array_search($upclHan2Arr[1],array(2,3,6,7,12,17,20))!==false)
											continue 2;
										$upclHan1Char=UTF8::Int2Char(Hangul::Organize($upclHan1Arr[0],$upclHan1Arr[1],$upclConvArr[$upclHan1Arr[2]][$upclHan2Arr[0]]));
										$upclHan2Char=UTF8::Int2Char(Hangul::Organize(11,$upclHan2Arr[1],$upclHan2Arr[2]));
										
										for($upclLoopIndex2Int=0;$upclLoopIndex2Int<6;$upclLoopIndex2Int++)
											$upclTextStr[$upclLoopIndexInt+$upclLoopIndex2Int]=$upclHan1Char[$upclLoopIndex2Int%3];
										unset($upclLoopIndex2Int);
									}
								}
								unset($upclHan1Arr,$upclHan2Arr);
								break;
						}
						unset($upclHan1Char,$upclHan2Char);
					}
					$upclLoopIndexInt+=3;
					break;
				default:
					$upclLoopIndexInt+=(($upclTmpCLInt=UTF8::GetCharLength($upclTextStr[$upclLoopIndexInt]))===0)?1:$upclTmpCLInt;
					break;
			}
		}
		unset($upclLengthInt,$upclConvArr,$upclLoopIndexInt,$upclTmpCLInt);
		
		return $upclTextStr;
	}
	
}

?>